<?php

namespace Admin\Controller;

/**
 * 课程维护
 *
 * @author gc
 */
class CourseController extends AdminController
{

    public function index()
    {
        $course = I('course');
        $type = I('type');
        $map['status'] = array('egt', 0);
        if (!empty($course)) {
            $map['title'] = array('like', '%' . (string) $course . '%');
        }

        if (!empty($type)) {
            $map['type'] = $type;
        }

        $list = $this->lists('Course', $map, 'id desc');
        $this->assign('_list', $list);

        $this->meta_title = '课程列表';
        $this->showAttr();
        $this->display();
    }

    private function showAttr()
    {
        $this->assign('_type', C("COURSE_TYPE"));
        $this->assign('_status', C("COURSE_STATUS"));
    }

    //改变课程状态
    public function editStatus()
    {
        $ids = I("id");
        $status = I("get.status");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => $status,
            'update_time' => time()
        ];

        $s = M("Course")->where($map)->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

    public function add()
    {
        $id = I("id");
        $meta_title = "新增课程";
        if (!empty($id)) {
            $map["id"] = $id;
            $data = M("Course")->where($map)->find();
            $this->assign("data", $data);
            $meta_title = "编辑课程";
        }
        $this->meta_title = $meta_title;
        $this->showAttr();
        $this->display();
    }

    public function save()
    {
        $data = I("post.");

        $id = I("id");
        if (empty($id)) {
            $data['status'] = 1;
            $data['create_time'] = $data['update_time'] = time();
            unset($data['id']);
        } else {
            $data['update_time'] = time();
        }

        $model = M("Course");
        $s = empty($id) ? $model->add($data) : $model->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

}
